/**
 * 登录
 * @author  Jello
 * @create  2018/10/15 16:21
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var spinner = new Spinner(opts);
var sessionStorage = window.sessionStorage;
var localStorage = window.localStorage;
var identity = null;
var withdraw_sign = localStorage.getItem("withdraw_sign"); //判断不同的用户
var detailId = ""; //截取分享招聘详情id

$(function() {
	//获取请求链接上的identity
	identity = getParam("identity");
	//若存在就更新identity，不存在就取缓存中
	if(identity) {
		sessionStorage.setItem("upIdentity", identity);
	} else {
		identity = sessionStorage.getItem("upIdentity");
	}

	//检测是否记住密码
	var loginSign = localStorage.getItem("phoneNum_local"); //获取本地存储手机号
	var pwdSign = localStorage.getItem("pwd_local"); //获取本地存储密码
	if(pwdSign) {
		$("#phoneNum").val(loginSign);
		$("#pwd").val($.base64.decode(pwdSign));
		$("#checkbox").attr("checked", true);
	} else {
		$("#phoneNum").val(loginSign);
		$("#pwd").val("");
		$("#checkbox").attr("checked", false);
	}

	//忘记密码
	$("#forgetPwd").click(function() {
		window.location.href = "forgetpassword.html";
	})
	//注册
	$("#mui_regist").click(function() {
		detailId = getParam('id');
		if(detailId) {
			window.location.href = "regist.html?upIdentity=" + identity + "&detailId=" + detailId.replace("#", "");
		} else {
			window.location.href = "regist.html?upIdentity=" + identity;
		}
	})

	//登录
	$("#mui_login").click(function() {
		var phoneNum = $("#phoneNum").val(); //手机号
		var pwd = $("#pwd").val(); //手机号

		if(checkPhone(phoneNum) && checkPwd(pwd, 1)) {
			var sendData = "{\"upIdentity\":\"" + identity + "\",\"phone\":\"" + phoneNum + "\",\"password\":\"" + hex_md5(pwd) + "\",\"type\":\"" + 0 + "\"}";

			$.ajax({
				type: "post",
				url: serverUrl + "/login/login",
				data: sendData,
				cache: false,
				async: false,
				contentType: "application/json; charset=utf-8",
				timeout: 15000,
				dataType: "json",
				crossDomain: true,
				beforeSend: function() {
					$("#myspin").text("");
					var target = $("#myspin").get(0);
					spinner.spin(target);
				},
				success: function(data) {
					spinner.spin();
					var resultObj = eval(data);
					//					console.log("登录接口：" + JSON.stringify(data));

					if(resultObj.code == 0) {
						if(resultObj.data == "null") {
							mui.toast("用户不存在");
							return;
						} else if(resultObj.data == "error") {
							mui.toast("密码错误");
							return;
						} else { //登录成功
							rememberPwd(); //回调记住密码
							var user_cookie = document.cookie; //获取浏览器cookie，正式上线用此方式获取
							//							var user_cookie = plus.navigator.getCookie(serverUrl); //真机调试
							//							console.log("用户cookie：" + user_cookie)
							var userid_sign = resultObj.data; //登录后返回的userid，去验证是否登录

							sessionStorage.setItem("user_cookie", user_cookie); //存储cookie，sessionStorage会话级
							sessionStorage.setItem("userid_sign", userid_sign); //临时存储该userid
							sessionStorage.setItem("detail_status", "1"); //分享链接-进详情的标识
							localStorage.setItem("withdraw_sign", userid_sign); //存储登录后的userid,便于验证不同用户登录后提现密码次数的问题

							if(userid_sign != withdraw_sign) { //不是当前用户清除十分钟次数和cookie的时间
								localStorage.removeItem("sess_time_local");
								$.cookie("sess_time_local", "", {
									expires: -1,
									path: '/bluehired'
								})
							}

							//判断是否从招聘详情链接登录
							if(sessionStorage.getItem("workdetail_sign") == 001155) { //如果有详情--作为返回的标识
								detailId = getParam('id'); //截取详情id,						
								window.location.href = "app/work/recruitmentlist_detailH5.html?id=" + detailId.replace("#", ""); //返回招聘详情带参id
							} 
//							else if(sessionStorage.getItem("wallet_sign") == 001166) { //进入我的钱包
//								window.location.href = "wallet.html";
//							} else if(sessionStorage.getItem("paycard_sign") == 001177) { //进入工资卡管理
//								window.location.href = "paycardmanager_grade.html";
//							} else if(sessionStorage.getItem("salary_sign") == 001188) { //进入工资明细
//								window.location.href = "salarybill.html";
//							} else if(sessionStorage.getItem("lend_sign") == 001199) { //进入借支
//								window.location.href = "lend.html";
//							} 
							else {
								window.location.href = "app/personal/personal.html";
							}

						}
					} else {
						mui.toast(resultObj.msg);
						return;
					}
				},
				error: function(XMLHttpRequest, status, errorThrown) {
					spinner.spin();
					if(status == 'timeout') {
						mui.toast("请求超时");
					} else {
						mui.toast("请求网络服务数据失败");
					}
					return;
				},
			});
		}
	})

});

/**
 * 记住密码
 */
function rememberPwd() {
	var phoneNum_local = $("#phoneNum").val(); //手机号
	var pwd_local = $("#pwd").val(); //密码
	var checked = $("#checkbox").prop('checked');
	if(checked) { //选中记住密码,永久存储本地
		localStorage.setItem("phoneNum_local", phoneNum_local);
		localStorage.setItem("pwd_local", $.base64.encode(pwd_local));
	} else {
		localStorage.removeItem("pwd_local");
	}
}

/**
 * 密码切换显示 
 */
document.getElementById("showPwd").addEventListener("tap", function() {
	var type = $("#pwd").prop("type")
	if("password" == type) {
		$("#pwd").prop("type", "text");
		$("#showPwd").attr("src", "img/login/regist_03.jpg");
	} else {
		$("#pwd").prop("type", "password");
		$("#showPwd").attr("src", "img/login/login_22.jpg");
	}
}, false)